package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MessageManager;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.vo.HuanXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/22 13:55
 * @Version 1.0
 */

@RestController
public class MessageController {

    @Autowired
    private MessageManager messageManager;

    //app获取用户的环信用户信息
    @GetMapping("/huanxin/user")
    public ResponseEntity findHuanxinUser() {

        Long userId = UserHolder.get().getId();

        HuanXinVo vo = new HuanXinVo();

        vo.setUsername("HX" + userId);
        vo.setPassword("123456");

        return ResponseEntity.ok(vo);
    }

    //向陌生人发送一条消息
    @PostMapping("/tanhua/strangerQuestions")
    public void strangerQuestions(@RequestBody Map<String, String> param) {

        Long jiarenId = Long.parseLong(param.get("userId"));

        String reply = param.get("reply");

        messageManager.strangerQuestions(jiarenId, reply);

    }

    //点击聊一下添加好友
    @PostMapping("/messages/contacts")
    public void addContacts(@RequestBody Map<String, Long> param) {

        Long friendId = param.get("userId");
        messageManager.addContacts(friendId);

    }

    //查询好友
    @GetMapping("/messages/contacts")
    public ResponseEntity findContactVoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String keyword
    ) {

        return messageManager.findContactVoByPage(pageNum, pageSize, keyword);

    }

    @Autowired
    private UserManager userManager;

    // 根据环信id查询用户头像和昵称
    @GetMapping("/messages/userinfo")
    public ResponseEntity findUserInfo(String huanxinId) {
        // 1.处理下环信id
        String userId = huanxinId.replace("HX", "");
        // 2.查询
        return userManager.findUserInfoVo(Long.parseLong(userId));

    }


    //查询点赞人的信息
    @GetMapping("/messages/likes")
    public ResponseEntity likes(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize

    ) {

        Integer commentType = 1;
        //调用manager
        return messageManager.findUserCommentVoByPage(commentType, pageNum, pageSize);
    }

    //查询评论人的信息
    @GetMapping("/messages/comments")
    public ResponseEntity comments(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize

    ) {

        Integer commentType = 2;
        //调用manager
        return messageManager.findUserCommentVoByPage(commentType, pageNum, pageSize);
    }

    //查询喜欢人的信息
    @GetMapping("/messages/loves")
    public ResponseEntity loves(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize

    ) {

        Integer commentType = 3;
        //调用manager
        return messageManager.findUserCommentVoByPage(commentType, pageNum, pageSize);
    }


    //查询公告
    @GetMapping("/messages/announcements")
    public ResponseEntity announcements(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {

        return messageManager.announcements(pageNum, pageSize);

    }




}
